<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>魔法方法2 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter06.html" />
    
    
    <link rel="prev" href="./chapter04.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="5"
        data-chapter-title="魔法方法2"
        data-filepath="chapter05.md"
        data-basepath="."
        data-revision="Wed Feb 19 2020 00:43:01 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        类和对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        面向对象基本语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        魔法方法1
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        魔法方法2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        内置属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="./chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        类属性和对象属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08.html">
            
                
                    <a href="./chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        私有属性和方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x8FD0;&#x7B97;&#x76F8;&#x5173;&#x7684;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;">&#x8FD0;&#x7B97;&#x76F8;&#x5173;&#x7684;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;</h2>
<p>&#x601D;&#x8003;:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age)</span>:</span>
    self.name = name
    self.age = age

p1 = Person(<span class="hljs-string">&apos;zhangsan&apos;</span>,<span class="hljs-number">18</span>)
p2 = Person(<span class="hljs-string">&apos;zhangsan&apos;</span>,<span class="hljs-number">18</span>)
print(p1 == p2)
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;<code>==</code>&#x8FD0;&#x7B97;&#x7B26;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x7ED3;&#x679C;&#x662F;True&#x8FD8;&#x662F;False?<code>==</code>&#x5230;&#x5E95;&#x6BD4;&#x8F83;&#x7684;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</p>
<h3 id="&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;&#x76F8;&#x5173;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;">&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;&#x76F8;&#x5173;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span>
        self.name = name
        self.age = age

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.name == other.name <span class="hljs-keyword">and</span> self.age == other.age

    <span class="hljs-comment"># def __ne__(self, other):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__lt__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.age &lt; other.age

    <span class="hljs-comment"># def __gt__(self, other):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__le__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.age &lt;= other.age
    <span class="hljs-comment"># def __ge__(self, other):</span>


s1 = Student(<span class="hljs-string">&apos;zhangsan&apos;</span>, <span class="hljs-number">18</span>)
s2 = Student(<span class="hljs-string">&apos;zhangsan&apos;</span>, <span class="hljs-number">18</span>)
s3 = Student(<span class="hljs-string">&apos;lisi&apos;</span>, <span class="hljs-number">20</span>)
print(s1 == s2)
print(s1 != s2)
print(s1 &gt; s2)
print(s1 &gt;= s2)
print(s1 &lt;= s2)
print(s1 &lt;= s2)
</code></pre>
<h3 id="&#x7B97;&#x6570;&#x8FD0;&#x7B97;&#x7B26;&#x76F8;&#x5173;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;">&#x7B97;&#x6570;&#x8FD0;&#x7B97;&#x7B26;&#x76F8;&#x5173;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span>
        self.name = name
        self.age = age

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__add__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.age + other

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__sub__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.age - other

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__mul__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.age * other

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__truediv__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.age / other

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__mod__</span><span class="hljs-params">(self, other)</span>:</span>
        <span class="hljs-keyword">return</span> self.age % other

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__pow__</span><span class="hljs-params">(self, power, modulo=None)</span>:</span>
        <span class="hljs-keyword">return</span> self.age ** power


s = Student(<span class="hljs-string">&apos;zhangsan&apos;</span>, <span class="hljs-number">18</span>)
print(s + <span class="hljs-number">1</span>)  <span class="hljs-comment"># 19</span>
print(s - <span class="hljs-number">2</span>)  <span class="hljs-comment"># 16</span>
print(s * <span class="hljs-number">2</span>)  <span class="hljs-comment"># 36</span>
print(s / <span class="hljs-number">5</span>)  <span class="hljs-comment"># 3.6</span>
print(s % <span class="hljs-number">5</span>)  <span class="hljs-comment"># 3</span>
print(s ** <span class="hljs-number">2</span>)  <span class="hljs-comment"># 324</span>
</code></pre>
<h3 id="&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x76F8;&#x5173;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;">&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x76F8;&#x5173;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span>
        self.name = name
        self.age = age

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__int__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self.age

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__float__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self.age * <span class="hljs-number">1.0</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self.name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__bool__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self.age &gt; <span class="hljs-number">18</span>


s = Student(<span class="hljs-string">&apos;zhangsan&apos;</span>, <span class="hljs-number">18</span>)
print(int(s))
print(float(s))
print(str(s))
print(bool(s))

<span class="hljs-keyword">if</span> s:
    print(<span class="hljs-string">&apos;hello&apos;</span>)
</code></pre>
<h3 id=""> </h3>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter04.html" class="navigation navigation-prev " aria-label="Previous page: 魔法方法1"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter06.html" class="navigation navigation-next " aria-label="Next page: 内置属性"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
